<?php
namespace app\api\controller;

use app\common\controller\Api;

class Citylord extends Api{
    protected $noNeedLogin = ['oldcity'];
    protected $noNeedRight = ['*'];

    public function getcity(){
        $userModel = new \app\admin\model\User();
        $provincewhere['province'] = ['neq',''];
        $provincelist = collection($userModel->where($provincewhere)->select());
        if($provincelist){
            $provincelist = $provincelist->toArray();
            $province_list = [];
            foreach ($provincelist as $k=>$v){
                array_push($province_list,$v['province']);
            }
        }else{
            $province_list = [];
        }
        $citywhere['city'] = ['neq',''];
        $citylist = collection($userModel->where($citywhere)->select());
        if($citylist){
            $citylist = $citylist->toArray();
            $city_list = [];
            foreach ($citylist as $k=>$v){
                array_push($city_list,$v['city']);
            }
        }else{
            $city_list = [];
        }
        $districtwhere['district'] = ['neq',''];
        $districtlist = collection($userModel->where($districtwhere)->select());
        if($districtlist){
            $districtlist = $districtlist->toArray();
            $district_list = [];
            foreach ($districtlist as $k=>$v){
                array_push($district_list,$v['district']);
            }
        }else{
            $district_list = [];
        }
        $backData = [
            'provincelist'=>$province_list,
            'citylist' => $city_list,
            'districtlist' => $district_list
        ];
        $this->success('成功',$backData);
    }

    public function setcity(){
        $province = $this->request->param('province');
        $city = $this->request->param('city');
        $district = $this->request->param('district');
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        if($userinfo['identity_id']!=2 && $userinfo['identity_id']!=3){
            $this->error('您还未达成成为城主或区长的条件');
        }
        if($userinfo['identity_id']==2){
            if($userinfo['district']!=''){
                $this->error('您已经是'.$userinfo['district'].'的区长，不能更换');
            }
            if($province=='' && $city=='' && $district==''){
                $this->error('成为区长省市区必传');
            }
            $userModel->where(['id'=>$userinfo['id']])->update([
                'province'=>$province,
                'city'=>$city,
                'district'=>$district,
            ]);
        }else if($userinfo['identity_id']==3){
            if($userinfo['city']!=''){
                $this->error('您已经是'.$userinfo['city'].'的城主，不能更换');
            }
            if($province=='' && $city==''){
                $this->error('成为城主省市必传');
            }
            $userModel->where(['id'=>$userinfo['id']])->update([
                'province'=>$province,
                'city'=>$city
            ]);
        }
        $this->success('成功');
    }
}